/*
 * nerdyCalendar JS Class
 * 
 * @author Philipp Datz
 * @author Ruben Müller
 *
 * @date 03/27/2008
 */



/*
 * The nCal (nerdyCalendar) class
 */
function nCal( targetDiv, dayArray )
{
	//console.log( 'nCal class object created.' )
}



/*
 * Defining the class variables
 */
nCal.prototype._targetDiv;
nCal.prototype._dayArray;
nCal.prototype._organizedDayArray;
nCal.prototype._hourHeight = 25;
nCal.prototype._dayWidth = 400;
nCal.prototype._maxParallels;



/*
 * Defining the class methods
 */
nCal.prototype.setup = function( targetDiv, dayArray )
{
	if( targetDiv && dayArray )
	{
		this._targetDiv = targetDiv;
		this._dayArray = dayArray;
	}
		else
	{
		console.error( 'You need to set all arguments for the nCal to work.' )
	}
}

nCal.prototype.entriesOrganize = function()
{
	var stA = new Array();
	var fillArray = new Array();
	var ouputArray = new Array();
	var maxParallels = new Number();
	
	stA = this._dayArray;
	stA.sort( startSort );
	
	var latestRow = 0;
	
	for (var i=0; i < stA.length; i++)
	{
		latestRow += 1;
		
		var object = new Object();
		object.id = stA[ i ].id;
		object.start = stA[ i ].start;
		object.end = stA[ i ].end;
		object.title = stA[ i ].title;
		object.parallel = 1;
		
		fillArray.push( object );
		for (var f=0; f < fillArray.length; f++)
		{	
			if( fillArray[ f ].end <= stA[ i ].start )
			{
				if( fillArray[ f ].row < latestRow) latestRow = fillArray[ f ].row;

				fillArray[ f ].width = fillArray[ f ].parallel;
				ouputArray.push( fillArray[ f ] );
				
				fillArray.splice( f, 1 );
				f--;
			}

			if(  fillArray[ f ] )
			{
				var count = fillArray.length;
				if( fillArray[ f ].parallel < count ) fillArray[ f ].parallel = count;
				if( this._maxParallels < count ) this._maxParallels = count;
				if( !fillArray[ f ].maxParallels ) maxParallels;
				if( !fillArray[ f ].row ) fillArray[ f ].row = latestRow;
			}
		}
	}
	
	for (var f=0; f < fillArray.length; f++)
	{
		if( fillArray[ f ].row < latestRow) latestRow = fillArray[ f ].row;
	
		fillArray[ f ].width = fillArray[ f ].parallel;
		ouputArray.push( fillArray[ f ] );
		
		fillArray.splice( f, 1 );
		f--;
	}

	this._organizedDayArray = ouputArray;
}

nCal.prototype.entriesUpdate = function()
{
	for (var i=0; i < this._organizedDayArray.length; i++)
	{
		//console.error( this._organizedDayArray[ i ].title + " / Width: " + this._organizedDayArray[ i ].width  + " / Row: " + this._organizedDayArray[ i ].row   + " / parallel: " + this._organizedDayArray[ i ].parallel );
		this.entryCreate
		(
			this._organizedDayArray[ i ].title,
			this._organizedDayArray[ i ].id,
			( this._dayWidth * ( 1 / this._organizedDayArray[ i ].width ) ),
			( this._organizedDayArray[ i ].start * this._hourHeight ),
			( this._organizedDayArray[ i ].end * this._hourHeight ),
			( this._organizedDayArray[ i ].row - 1 ) * ( this._dayWidth / this._organizedDayArray[ i ].parallel )
		);
	};
}

nCal.prototype.entryAdd = function( id, start, end, title )
{
	var object = new Object;
	object.id = stA[ i ].id;
	object.start = stA[ i ].start;
	object.end = stA[ i ].end;
	object.title = stA[ i ].title;
	
	this._dayArray.push( object );
	
}

nCal.prototype.entryCreate = function( title, id, width, start, end, left )
{
	//if( id && width && start && end )
	//{
		eventDiv = this.entryDivCreate( id, left );
		eventDiv[ 0 ].setHTML( title );
		
		eventDiv[ 0 ].id = id;
		eventDiv[ 0 ].style.width = width;
		eventDiv[ 0 ].style.top = start;
		eventDiv[ 0 ].style.height = end - start;
	
		eventDiv[ 0 ].injectInside( this._targetDiv );
		eventDiv[ 1 ].injectInside( eventDiv[ 0 ] );
		eventDiv[ 2 ].injectInside( eventDiv[ 0 ] );
		
		this.entryMakeInterative( eventDiv[ 0 ] );
	//}
	//	else
	//{
	//	console.error( 'You need to set all arguments for the entryCreate method to work.' )
	//}
}

nCal.prototype.entryDivCreate = function( id, left )
{
	var eventDiv = new Element
	(
		'div',
		{
			'styles':
			{
				'left': left,
			},
			'class': 'entry'
		}
	);
	
	var eventHandler = new Element
	(
		'div',
		{
			'id': 		'handler' + id,
			'class': 	'handler'   
		}
	);
	
	var eventDeleter = new Element
	(
		'div',
		{
			'onclick': 	'deleteEntry( $( this.parentNode ).id )',
			'class': 	'delete'   
		}
	);
	
	var returnArray = new Array( eventDiv, eventHandler, eventDeleter );
	return returnArray;
}

nCal.prototype.entryMakeInterative = function( target )
{
	if( target )
	{
		$( target ).makeResizable
		(
			{
				grid: 25 / 2, 
				modifiers:
				{
					x: false,
					y: 'top'
				}, 
				limit:
				{
					y: [ 0, 24 * 25 ]
				} 
			}
		);
		
		$( target ).makeResizable
		(
			{
				grid: 25 / 2, 
				modifiers:
				{
					x: false,
					y: 'height'
				},
				handle: 'handler' + target.id,
				limit:
				{
					y: [ 0, 24 * 25 ]
				} 
			}
		);
	}
		else
	{
		console.error( 'You need to set a target for the entryMakeInterative method to work.' )
	}
}

nCal.prototype.entryDelete = function( target )
{
	$( target ).effect
	(
		'opacity',
		{
			duration: 100
		}
	).start( .8, 0 ).addEvent
	(
		'onComplete',
		function()
		{
			$( target ).remove();
		}	
	);
}



/*
 * Defining some other functions
 */
function startSort( a, b )
{
	return a.start - b.start;
}